package com.example.lowbird.lowbird.activity;

import android.content.Context;
import android.content.Intent;
import android.provider.SyncStateContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.app.Myapp;
import com.example.lowbird.lowbird.bean.Logrbean;
import com.example.lowbird.lowbird.constant.Contants;
import com.example.lowbird.lowbird.utils.SpUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
import okhttp3.Call;


public class RegistActivity extends AppCompatActivity {

    private EditText et_login;
    private EditText et_logword;
    private Button bt_login;
    private TextView tv_login;
    private TextView tv_word;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        mContext=this;
        initview();

    }

    private void initview() {

        et_login = ((EditText) findViewById(R.id.et_loginphone));

        et_logword = ((EditText) findViewById(R.id.et_loginpassword));

        bt_login = ((Button) findViewById(R.id.bt_login));

        tv_login = ((TextView) findViewById(R.id.tv_toregist));

        tv_word = ((TextView) findViewById(R.id.tv_forgetword));

    iniiclicklistenser();


    }

    private void iniiclicklistenser() {
        //设置注册监听事件
        tv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mContext.startActivity(new Intent(mContext,LoginActivity.class));

            }
        });

        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String phone = et_login.getText().toString().trim();
                if (TextUtils.isEmpty(phone)){

                    Log.e("LOG", "请输入正确的手机号 " );
                    return;
                }


                String password = et_logword.getText().toString();
                if (TextUtils.isEmpty(password)){

                    Log.e("LOG", "请输入正确的手机号 " );
                    return;
                }



                Map<String,String> params=new HashMap<String, String>(2);

                params.put("phone",phone);

                params.put("password",password);

                OkHttpUtils.post().url(Contants.API.LOGIN).params(params).build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {

                            }

                            @Override
                            public void onResponse(String response, int id) {
                                Log.e("LOG", "onResponse: "+response );

                                String string = response.toString();

                                Logrbean logrbean = new Gson().fromJson(string, Logrbean.class);
                                if (logrbean.getStatus()==1) {
                                    if (Myapp.getmInsance().getIntent()!=null){

                                        Myapp.getmInsance().jumpToTargetActivity(mContext);


                                    }else
                                    {


                                    SpUtil.putString(mContext, phone, string);
                                    SpUtil.putString(mContext, Contants.ifRLOGIN, phone);
                                    Intent intent = new Intent(mContext, MainActivity.class);
                                    intent.putExtra("intent", "regist");
                                    startActivity(intent);}
                                    finish();
                                }
                            }
                        });


            }
        });


    }
}
